set more off
local a 23
local y 1981

*** OUTCOMES FROM SURVEY DATA FOR AGE 23
use "Data/NCDS/UKDA-5566-stata9/stata9/ncds4.dta", clear

rename ncdsid NCDSID

* merge RPI
ge year=`y'
merge m:1 year using scratchdata/RPI.dta, keep(match) nogenerate

** sex
rename n622_4 sex_`a'

** Observation indicator
ge Obs_23=1

rename n4region region_`a'

********************************************************************************
* Part 1: pay period for employed 
********************************************************************************

* net (current)
ge pp_nee=n4267 if n4267>0 
recode pp_nee (1=0.2) (2=1) (3=2) (4=4.33) (5=13) (6=26) (7=52)

* gross (current)
ge pp_gee=n4274  if n4274>0 
recode pp_gee (1=0.2) (2=1) (3=2) (4=4.33) (5=13) (6=26) (7=52)

********************************************************************************
* Part 2: pay for employed
********************************************************************************

* net 
ge npee_`a'=n4262

* gross
ge gpee_`a'=n4269

********************************************************************************
* Part 3: pay per week for employed
********************************************************************************

* net 
replace npee_`a'=npee_`a'/pp_nee 

* gross
replace gpee_`a'=gpee_`a'/pp_gee 

********************************************************************************
* Part 4: hours per week for employed
********************************************************************************

ge hoursee_`a'=n4331
recode  hoursee_`a' (97=.)

********************************************************************************
* Part 5: weekly earnings for self-employed
********************************************************************************

ge ese_`a'= npee_`a'  	/* many self employed answered earning question*/
ge npse_`a'=.   		/* information not collected*/

********************************************************************************
* Part 6: hours per week for self-employed
********************************************************************************

ge hoursse_`a'=n4331			/* same hours question asked of employed and self-employed*/
recode  hoursse_`a' (97=.)

********************************************************************************
* Part 7: inflation adjustment
********************************************************************************

local vars npee_`a' gpee_`a' ese_`a' npse_`a'
foreach v of local vars {
replace `v'=`v'/rpi
}

********************************************************************************
* Part 8: Economic activity
********************************************************************************

rename econstat EconAct
recode EconAct (1=6) (2 4=1) (3=2) (0=.) (5 6 7=5)
replace EconAct=3 if EconAct==1 & dempstat==2
replace EconAct=4 if EconAct==2 & dempstat==2

ge EE_`a'=(EconAct==1|EconAct==2) if EconAct!=. 
ge SE_`a'=(EconAct==3|EconAct==4) if EconAct!=. 

* clasify as non-working if report zero earnings
replace EE_`a'=0 if npee_`a'==0 & EE_`a'!=.
replace SE_`a'=0 if ese_`a'==0  & SE_`a'!=.
ge AE_`a'=EE_`a'+SE_`a'

ge UN_`a'=EconAct==5 if EconAct!=.

********************************************************************************
* Part 9: recode hours
********************************************************************************

* recode hours to missing if hours>112 or zero
replace hoursee_`a'=. if (hoursee_`a'>112|hoursee_`a'==0)
replace hoursse_`a'=. if (hoursse_`a'>112|hoursse_`a'==0)

replace hoursee_`a'=40 if EconAct==1 & hoursee_`a'==.
replace hoursee_`a'=20 if EconAct==2 & hoursee_`a'==.
replace hoursse_`a'=40 if EconAct==3 & hoursse_`a'==.
replace hoursse_`a'=20 if EconAct==4 & hoursse_`a'==.

replace hoursee_`a'=. if  npee_`a'==.
replace hoursse_`a'=. if  ese_`a'==.

* recode earnings and hours as missing if don't match current economic activity
replace npee_`a'=. if EE_`a'!=1
replace gpee_`a'=. if EE_`a'!=1
replace hoursee_`a'=. if  EE_`a'!=1
replace ese_`a'=. if SE_`a'!=1
replace hoursse_`a'=. if SE_`a'!=1

********************************************************************************
* Part 10: hourly wages
********************************************************************************

* employed net
ge wageee_`a'=npee_`a'/hoursee_`a'

* employed gross
ge gwageee_`a'=gpee_`a'/hoursee_`a'

* self-employed (earnings)
ge wagese_`a'=ese_`a'/hoursse_`a'

* self-employed (profit) 
ge prose_`a'=npse_`a'/hoursse_`a'

********************************************************************************
* Part 11: combine employed and self-employed
********************************************************************************

egen wage_`a'=rowmax(wageee_`a' wagese_`a')
drop wageee_`a' wagese_`a'

egen pay_`a'=rowmax(npee_`a' ese_`a')
drop npee_`a' ese_`a'

egen hours_`a'=rowmax(hoursee_`a' hoursse_`a')
drop hoursee_`a' hoursse_`a'

********************************************************************************
*  Part 12: log variables
********************************************************************************

local vars wage pay hours
foreach v of local vars {
ge l`v'_`a'=ln(`v'_`a')
}

********************************************************************************
* Part 13: SOC 1980
********************************************************************************

ge soc80_`a'=int(n6138/100) /* first 3 digits are the Condensed KOS code, bassed on the Classification of Occupations 1980*/ 
replace soc80_`a'=. if AE_`a'!=1

ge CreatOcc_`a' = . 

********************************************************************************
* Part 15: Job security
********************************************************************************

ge JobSecurity_`a'=n4322
replace JobSecurity_`a'=. if JobSecurity_`a'==8|AE_`a'!=1
ge JobSecurityHi_`a'=JobSecurity_`a'==1 if JobSecurity_`a'!=.
ge JobSecurityMedHi_`a'=JobSecurity_`a'==1|JobSecurity_`a'==2 if JobSecurity_`a'!=.

********************************************************************************
* Part 16: Industry
********************************************************************************

* current or last job
rename n6145 Ind_`a' /* 1980 SIC code */
recode Ind_`a' (-1=.)

* first job 
rename n6128  Indf_`a'
recode Indf_`a' (-1=.)

* recode current as first if no current and still in first
replace Ind_`a'= Indf_`a' if  Indf_`a'!=. & n4152==1
drop Indf_`a'

replace Ind_`a'=floor(Ind_`a'/10)
label define IIInd_`a' 0 "Agriculture" 1 "Energy and water supply" 2 "mining metals and chem" 3 "metals, engineering, vech" ///
4 "Other manuf" 5 "Construction" 6 "Distribution, hotels, catering, repairs" 7 "Transport and comm"  8 "Banking and finance" 9 "Other serives" 

* Indicators
ta Ind_`a', ge(IInd_`a')
rename IInd_`a'* IInd*_`a'

********************************************************************************
* Part 17: education (level 4 and 5 quals)
**************************************************************************

* university qualification
egen UniQual_`a'=anymatch(n4529 n4573 n4622 n4634), values(27 29 30 31)

* alternative measure
egen RUniQual_`a'=anymatch(n4529 n4573 n4622 n4634), values(29 30 31)

* vocational quals
egen HighVocQual_`a'=anymatch(n4529 n4573 n4622 n4634), values(17 21 23 23 25 26)


********************************************************************************
* Characteristics of currrent job
********************************************************************************

* had training
rename n4246 JobHadTrain_`a'
recode JobHadTrain_`a' (9=.) (2=0)

* had extensive training
ge  JobHadETrain_`a'= JobHadTrain_`a'
replace JobHadETrain_`a'=0 if n4248==1

* had training course
ge  JobHadCTrain_`a'= JobHadTrain_`a'
replace JobHadCTrain_`a'=0 if n4249==2

* promotion prospects
rename n4321 JobPromP_`a'
recode JobPromP_`a' (2=0) (3 8=.)

* Expect to move employers
rename n4323 JobExMv_`a'
recode JobExMv_`a' (1=0) (2=1) (3=.) (8=0) (9=.)

********************************************************************************
* Apprenticeship
********************************************************************************

rename n4424 StApp_`a'
recode StApp_`a' (2=0) (9=.)

ge FiApp_`a'=StApp_`a'
replace FiApp_`a'=0 if  n4439~=1 & StApp_`a'~=.

********************************************************************************
* Skills and training required for current job
********************************************************************************

* current job
replace n6262=. if n6262==99|n6262==40
replace n6264=. if n6264==99|n6264==40
replace n6266=. if n6266==99|n6266==40

* first job
replace n6413=. if n6413==99|n6413==40
replace n6415=. if n6415==99|n6415==40
replace n6417=. if n6417==99|n6417==40

replace n6262=n6413 if n6262==.
replace n6264=n6415 if n6264==.
replace n6266=n6417 if n6266==.

egen nm=rowmiss(n6262 n6264 n6266)

replace n6262=1 if n6262==. & nm<3
replace n6264=1 if n6264==. & nm<3
replace n6266=1 if n6266==. & nm<3

replace n6262=. if AE_`a'!=1
replace n6264=. if AE_`a'!=1
replace n6266=. if AE_`a'!=1

* Requires experience
egen SkExp_`a'=anymatch(n6262 n6264 n6266), values(24)
replace SkExp_`a'=. if n6262==.

* Requires training or apprenticeship
egen SkTr_`a'=anymatch(n6262 n6264 n6266), values(20 23 25)
replace SkTr_`a'=. if  n6262==.

* requires formal qualification
egen SkFq_`a'=anymatch(n6262 n6264 n6266), values(2 3 4 5 6 7 8 9 10)
replace SkFq_`a'=. if  n6262==.

********************************************************************************
* Finish up
********************************************************************************

keep NCDSID *_`a'
sort NCDSID
rename *_`a' *_I`a'

save scratchdata/ncds_1981_flatfile.dta, replace



/*

* recode failed qualification attempts
replace n4573 =. if n4575~=1
replace n4622=.  if n4624~=1
replace n4634=.  if n4636~=1

* vocational level 2 qualifications (RSA, C&G, OND, ONC)
egen Voc2_`a'=anymatch(n4529 n4573 n4622 n4634), values(11 12 13 14 15 16 17 18 19 20)
replace Voc2_`a'=. if AttQual==.

* Non-degree higher qual  
egen HQual_`a'=anymatch(n4529 n4573 n4622 n4634), values(21 22 23 24 25 26 27 28)
replace HQual_`a'=. if AttQual==.

* Degree or graduate qual.  
egen Degree_`a'=anymatch(n4529 n4573 n4622 n4634), values(29 30 31)
replace Degree_`a'=. if AttQual==.

* Age left school
rename  agelsch  agelsch_`a'
recode agelsch_`a' (1=16) (2=17) (3=18)

egen Qual1=anymatch(n4529 n4573 n4622 n4634), values(1 2 3 11 22)
egen Qual2=anymatch(n4529 n4573 n4622 n4634), values(4 5 7 12 13 14 16 19)
egen Qual3=anymatch(n4529 n4573 n4622 n4634), values(6 8 9 16 20 23)

* passed o-levels or CSE grade 1
rename n4655 OLevels_`a'
recode OLevels_`a' (9=.) (2=0) 

* number of o-levels or CSE grade 1 passes
rename n4656 NOLevels_`a'
replace NOLevels_`a'=0 if OLevels_`a'==0

* recode for consistency
replace OLevels_`a'=1 if NOLevels_`a'>0 &  NOLevels_`a'!=. 
replace NOLevels_`a'=0 if OLevels_`a'==0 &  NOLevels_`a'==.

* passed a levels
rename n4657 ALevels_`a'
recode ALevels_`a' (9=.) (2=0) 

* number of a levels passed
rename n4658 NALevels_`a'
replace ALevels_`a'=1 if NALevels_`a'>0 & NALevels_`a'!=.
replace NALevels_`a'=0 if ALevels_`a'==0 &  NALevels_`a'==.

* other qualification attempts
ge AttQual=1 if (n4526==1|n4527==1)
replace AttQual=0 if AttQual==. & (n4526!=.|n4527!=.)

ge HiQual_`a' = 0 if AttQual~=.

/*
********************************************************************************
* Part 14: Occupational attainment
********************************************************************************

rename n6149 SC_`a'
replace SC_`a'=n6132 if SC_`a'==. & n4152==1 /* use first job SC if still in first job and current job SC missing */
replace SC_`a'=. if SC_`a'>=60
replace SC_`a'=. if AE_`a'!=1
recode SC_`a' (11=1) (21=2) (31=3.1) (32=3.2) (41 42=4) (52=5)

ge SC_NM_`a'=1 if SC_`a'>=1 & SC_`a'<=3.15
replace SC_NM_`a'=0 if SC_NM_`a'==. & SC_`a'!=.

ge SC_ProHMT_`a'=1 if SC_`a'>=1 & SC_`a'<=2
replace SC_ProHMT_`a'=0 if SC_ProHMT_`a'==. & SC_`a'!=.
*/
